package com.zxmclasslibrarie.view.wtflist;

import java.lang.ref.SoftReference;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.zxmclasslibrarie.network.about.ImageDownloaderManager;
import com.zxmclasslibrarie.system.about.CharsUtil;
import com.zxmclasslibrarie.system.about.PreferencesUtil;
import com.zxmclasslibrarie.system.cache.Cache;
import com.zxmclasslibrarie.view.wtflist.WeiboListWidget.OnWeiboListOnClickListenner;

public class WeiboListAdapter extends BaseAdapter {
	private final String TAG = "WeiboListAdapter";

	private static HashMap<Integer, SoftReference<WeiboListIView>> hashCatch = null;
	private List<WeiBoEntity> listOfWeiBo = null;
	private Cache mCache = null;
	private Context mContext = null;
	private int page = 0;

	public WeiboListAdapter(Context context, WeiBoListEntity weibo,
			List<WeiBoEntity> list) {
		this.page = weibo.getPage();
		this.listOfWeiBo = list;
		this.mContext = context;
		// this.mCache = Cache.getInstance(App.context,
		// Constant.IAMGE_FILE_NAME);
		if (null == hashCatch) {
			WeiboListAdapter.hashCatch = new HashMap<Integer, SoftReference<WeiboListIView>>();
		}
	}

	@Override
	public int getCount() {
		return listOfWeiBo.size();
	}

	@Override
	public WeiboListIView getItem(int position) {
		return bindView(position);
	}

	public int getItemHeight(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return null;
	}

	/**
	 * 取到item中的控件
	 * 
	 * @param position
	 * @param rowView
	 */
	public WeiboListIView bindView(int position) {
		Log.v(TAG, "position===" + position);
		WeiboListIView listView = null;
		WeiBoEntity entity = listOfWeiBo.get(position);

		SoftReference<WeiboListIView> view = hashCatch.get(position);
		if (null != view && null != view.get()) {
			listView = view.get();
		} else {
			listView = new WeiboListIView(mContext, position);
			hashCatch
					.put(position, new SoftReference<WeiboListIView>(listView));
		}

		TextView text = listView.getMtTextView();
		ImageView image = listView.getmImageView();
		listView.height = getDateHeight(entity);
		setViewVie(entity, text);
		setViewP(image, entity);
		bindDate(text, image, entity);
		return listView;
	};

	private ImageView setViewP(ImageView imageView, WeiBoEntity entity) {
		double height = entity.getMpich();
		double width = entity.getMpicw();
		int listWidth=0;
//		int listWidth = PreferencesUtil.getInstance().getInt(
//				Constant.LIST_WIDTH);
//		list的宽度，在软件开始时就已经计算完

		if (width != 0 && width < listWidth) {
			imageView.getLayoutParams().width = -2;
			imageView.getLayoutParams().height = -2;
		} else {
			NumberFormat f = NumberFormat.getNumberInstance();
			f.setMaximumFractionDigits(2);
			double d = listWidth / width;
			int listheight = (int) (height * d);
			imageView.getLayoutParams().width = -1;
			imageView.getLayoutParams().height = listheight;
		}
		return imageView;
	}

	private void bindDate(TextView textView, ImageView imageView,
			WeiBoEntity entity) {
		textView.setText(entity.getText());
		String path = entity.getMpic();

		if (!path.equals("")) {
			ImageDownloaderManager.newListDownInstance(mContext, path).downloadBookCover(
					path, imageView, false);
			Log.v("xxx", "path=" + path);
			// mCache.loadBitmap(path, imageView);
		}
	}

	/**
	 * 如果没有数据，相应View隐藏
	 * 
	 * @param entity
	 * @param textView
	 * @param imageView
	 */
	private void setViewVie(WeiBoEntity entity, TextView textView) {
		RelativeLayout.LayoutParams p = (LayoutParams) textView
				.getLayoutParams();
		if (entity.getText().equals("")) {
			p.height = 0;
			p.topMargin = CharsUtil.dipTopx(mContext, 9);
		} else {
			p.height = -2;
			p.topMargin = CharsUtil.dipTopx(mContext, 5);
		}
	}

	public void setItemListnener(OnWeiboListOnClickListenner listenner) {
		for (int i = 0; i < hashCatch.size(); i++) {
			SoftReference<WeiboListIView> view = hashCatch.get(i);
			if (null != view && null != view.get()) {
				view.get().setItemListenner(listenner);
			} else {
				WeiboListIView listView = new WeiboListIView(mContext, i);
				listView.setItemListenner(listenner);
				hashCatch.put(i, new SoftReference<WeiboListIView>(listView));
			}
		}
	}

	/**
	 * 清除list缓存
	 */
	public void clearMashMap() {
		hashCatch.clear();
	}

	/**
	 * 获得当前page
	 * 
	 * @return
	 */
	public int getPage() {
		return page;
	}

	/**
	 * 根据返回的textlength来计算高度
	 * 
	 * @param entity
	 * @return
	 */
	private int getDateHeight(WeiBoEntity entity) {
		String text = entity.getText();
		int picH = entity.getMpich();
		int textH = (text.length() * 12);
		return picH + textH;
	}
}
